package com.eso.frame.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * User: Noah.Rong
 * Version: 0.1,  2012-07-28 11:00
 */
public final class DateUtils {


    public static String getNow() {
        return getDefaultDateTime(new Date());
    }

    public static String getCurrentDate() {
        return getDefaultDate(new Date());
    }

    public static String getDefaultDateTime(Date date) {
        date = getDateTimeParameter(date);
        return getDateTimeFormatter(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getDefaultDateTimeWithNoSplit(Date date) {
        date = getDateTimeParameter(date);
        return getDateTimeFormatter(date, "yyyyMMddHHmmss");
    }

    public static String getDefaultDate(Date date) {
        date = getDateTimeParameter(date);
        return getDateTimeFormatter(date, "yyyy-MM-dd");
    }

    public static String getDefaultDateWithNoSplit(Date date) {
        date = getDateTimeParameter(date);
        return getDateTimeFormatter(date, "yyyyMMdd");
    }

    private static Date getDateTimeParameter(Date date) {
        return (null!=date ? date : new Date());
    }

    private static String getDateTimeFormatter(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * @param m (m=0本日 m=1明天,-1 昨天)
     * @return str
     */
    public static Date getDateBefore(int m) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, m);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);


        return cal.getTime();
    }


    /**
     * 得到小时和分钟
     *
     * @param d 日期
     * @return 15：32
     */
    public static String getTime(Date d) {
        Calendar cal = Calendar.getInstance();

        cal.setTime(d);
        String mi = "";

        mi = cal.get(Calendar.MINUTE) < 10 ? "0" + cal.get(Calendar.MINUTE) : "" + cal.get(Calendar.MINUTE);

        return cal.get(Calendar.HOUR_OF_DAY) + ":" + mi;
    }


    /**
     * 两个日期间差值
     *
     * @param from       开始时间
     * @param to         结束时间
     * @param showSecond 是否显示秒
     * @return x天x小时x分x秒
     * @editor:RongWei
     * @editDate:2010-11-02
     */
    public static String getTimeDis(String from, String to, boolean showSecond) {

        StringBuffer buff = new StringBuffer();
        SimpleDateFormat dfs = new SimpleDateFormat("yyyyMMddHHmmss");
        Date begin = null;
        Date end = null;
        try {
            begin = dfs.parse(from);
            end = dfs.parse(to);
            long between = (end.getTime() - begin.getTime()) / 1000;
            long day1 = between / (24 * 3600);
            long hour1 = between % (24 * 3600) / 3600;
            long minute1 = between % 3600 / 60;
            long second1 = between % 60;

            if (day1 > 0) {
                buff.append(addZeroToNum(String.valueOf(day1), 2));
                buff.append("天");
                return buff.toString();
            }
            if (hour1 > 0) {
                buff.append(addZeroToNum(String.valueOf(hour1), 2));
                buff.append("时");
                return buff.toString();
            }
            if (minute1 > 0) {
                buff.append(addZeroToNum(String.valueOf(minute1), 2));
                buff.append("分");
                return buff.toString();
            }
            if (second1 > 0 & showSecond) {
                buff.append(addZeroToNum(String.valueOf(second1), 2));
                buff.append("秒");
                return buff.toString();
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return buff.toString();
    }

    /**
     * 将数字num补充为指定长度length的字符，如 1 补为001 或01 等
     *
     * @param num    需要补充的“数字”
     * @param length 补充长度
     * @return 补充后的字符 如 02,003等
     * @editor:RongWei
     * @editDate:2010-11-02
     */
    private static String addZeroToNum(String num, int length) {

        StringBuffer rst = new StringBuffer();

        int l = num.length();

        for (int i = 0; i < (length - l); i++) {
            rst.append("0");
        }

        rst.append(num);

        return rst.toString();

    }
}
